﻿
//Mirco Töpfer


#region Usings
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using pheidippides.common.Classes;
using pheidippides.configuration.Classes;
using pheidippides.dataaccess.Classes;
using pheidippides.logging.Classes;

#endregion

namespace pheidippides.business.Classes
{
    public class Strecke : INotifyPropertyChanged
    {
        #region Private Members
        private readonly StreckeData _streckeData;
        #endregion

        #region Constructor


        /// <summary>
        /// Prevents a default instance of the <see cref="Strecke"/> class from being created.
        /// </summary>
        private Strecke()
        {
            Logger.GetInstance().LogMessage("Generierung neue Strecke", LogLevel.Debug);

            _streckeData = new StreckeData();
            _streckeData.Id = Guid.NewGuid();
            _streckeData.Name = "";
            _streckeData.Notiz = "";
            _streckeData.StartzeitTicks = null;
            _streckeData.VeranstaltungFk = ConfigManager.GetInstance().GetVeranstaltungsId();
        }

        /// <summary>
        /// Prevents a default instance of the <see cref="Strecke"/> class from being created.
        /// </summary>
        /// <param name="data">The data.</param>
        private Strecke(StreckeData data)
        {
            _streckeData = data;
        }

        #endregion

        #region Public Accessors

        /// <summary>
        /// Gets the id.
        /// </summary>
        public Guid Id
        {
            get { return _streckeData.Id; }
        }

        /// <summary>
        /// Gets or sets the name.
        /// </summary>
        /// <value>
        /// The name.
        /// </value>
        public String Name
        {
            get { return _streckeData.Name; }
            set { _streckeData.Name = value; }
        }

        /// <summary>
        /// Gets or sets the notiz.
        /// </summary>
        /// <value>
        /// The notiz.
        /// </value>
        public String Notiz
        {
            get { return _streckeData.Notiz; }
            set { _streckeData.Notiz = value; }
        }

        /// <summary>
        /// Gets or sets the startzeit. There is a mappig from Datetime to long because the MS Access DB doesn't handle with the complete c# datetime range!
        /// </summary>
        /// <value>
        /// The startzeit.
        /// </value>
        public DateTime? Startzeit
        {
            get
            {
                if (_streckeData.StartzeitTicks != null)
                    return new DateTime(_streckeData.StartzeitTicks.Value);
                return null;
            }
            set
            {
                if (value != null)
                    _streckeData.StartzeitTicks = value.Value.Ticks;
                else
                _streckeData.StartzeitTicks = null;
            }
        }

        /// <summary>
        /// Gets or sets the veranstaltung.
        /// </summary>
        /// <value>
        /// The veranstaltung.
        /// </value>
        public Veranstaltung Veranstaltung
        {
            get { return Veranstaltung.GetById(_streckeData.VeranstaltungFk); }
            set { _streckeData.VeranstaltungFk = value.Id; }
        }

        #endregion

        #region Factory Classes

        /// <summary>
        /// Gets the new.
        /// </summary>
        /// <returns></returns>
        public static Strecke GetNew()
        {
            return new Strecke();
        }

        /// <summary>
        /// Gets all.
        /// </summary>
        /// <returns></returns>
        public static List<Strecke> GetAll()
        {
            return GetStreckeFromStreckeData(StreckeDataAccess.GetAll(ConfigManager.GetInstance().GetVeranstaltungsId()));
        }

        /// <summary>
        /// Gets the by id.
        /// </summary>
        /// <param name="id">The id.</param>
        /// <returns></returns>
        public static Strecke GetById(Guid id)
        {
            StreckeData data = StreckeDataAccess.Get(id, ConfigManager.GetInstance().GetVeranstaltungsId());
            
            if (data == null) 
                return null;

            return new Strecke(data);
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Saves this instance.
        /// </summary>
        public void Save()
        {
            StreckeDataAccess.Save(_streckeData);
        }

        /// <summary>
        /// Deletes this instance.
        /// </summary>
        public void Delete()
        {
            StreckeDataAccess.Delete(_streckeData);
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Gets the strecke from strecke data.
        /// </summary>
        /// <param name="streckeData">The strecke data.</param>
        /// <returns></returns>
        private static List<Strecke> GetStreckeFromStreckeData(IEnumerable<StreckeData> streckeData)
        {
            List<Strecke> strecken = new List<Strecke>();

            foreach (StreckeData data in streckeData)
            {
                strecken.Add(new Strecke(data));
            }

            return strecken;
        }

        #endregion

        #region INotifyPropertyChanged 

        /// <summary>
        /// Tritt ein, wenn sich ein Eigenschaftenwert ändert.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
    }
}
